package com.mvpdemo;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;

import com.google.android.material.textfield.TextInputEditText;
import com.mvpdemo.bean.User;
import com.mvpdemo.mvp.IUserLoginView;
import com.mvpdemo.mvp.UserLoginPresenterImpl;

import java.util.ArrayList;
import java.util.Objects;

/**
 * @link https://www.jianshu.com/p/2ae07b498356
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener, IUserLoginView {

    private static final String TAG = "MainActivity";

    private TextInputEditText nameEt, pwdEt;
    private UserLoginPresenterImpl presenter;
    private RecyclerView contentRv;
    private ArrayList<User> userArrayList;
    private RvAdapter rvAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        nameEt = findViewById(R.id.name_et);
        pwdEt = findViewById(R.id.pwd_et);

        AppCompatButton absoluteBtn = findViewById(R.id.absolute_btn);
        absoluteBtn.setOnClickListener(this);

        AppCompatButton cancelBtn = findViewById(R.id.cancel_btn);
        cancelBtn.setOnClickListener(this);

        contentRv = findViewById(R.id.rv_content);
        contentRv.setLayoutManager(new LinearLayoutManager(this));
        userArrayList = new ArrayList<>();
        rvAdapter = new RvAdapter(this, userArrayList);
        contentRv.setAdapter(rvAdapter);


        presenter = new UserLoginPresenterImpl(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.absolute_btn:
                presenter.doLogin();
                break;
            case R.id.cancel_btn:
                presenter.clear();
                break;
        }
    }

    @Override
    public String getUserName() {
        Editable text = nameEt.getText();
        return text == null ? null : text.toString();
    }

    @Override
    public String getPassWord() {
        Editable text = pwdEt.getText();
        return text == null ? null : text.toString();
    }

    @Override
    public void clearUserName() {
        nameEt.setText("");
    }

    @Override
    public void clearPwd() {
        pwdEt.setText("");
    }

    @Override
    public void showLoading() {
        Log.e(TAG, "showLoading: ");
    }

    @Override
    public void hiddenLoading() {
        Log.e(TAG, "hiddenLoading: ");
    }

    @Override
    public void toMainActivity() {
        Log.e(TAG, "toMainActivity: ");
    }

    @Override
    public void showLoginFailed() {
        Log.e(TAG, "showLoginFailed: ");
    }
}
